package lengxf;

import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.*;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import javax.sql.DataSource;
import java.util.stream.IntStream;

public class Main {

  /**
   * 直接参考官方文档
   * 使用官方最简单配置 进行源码阅读
   * {@see https://mybatis.org/mybatis-3/zh/getting-started.html}
   *
   * @author Lengxf
   */
  public static void main(String[] args) {
    //数据源
    DataSource dataSource = BlogDataSourceFactory.getBlogDataSource();
    //事务实现工厂
    TransactionFactory transactionFactory = new JdbcTransactionFactory();
    //mybatis运行时环境
    Environment environment = new Environment("development", transactionFactory, dataSource);
    //mybatis的配置
    //与mybatis-config.xml的作用相同
    //这里 不演示xml形式的代码
    Configuration configuration = new Configuration(environment);

    configuration.setLogImpl(StdOutImpl.class);

      //    ==>  Preparing: INSERT INTO `test_1`.`blog` (`id`, `name`) VALUES (?, ?);
      //==> Parameters: 1(String), lengxf1(String)
      //      <==    Updates: 1
      //      ==>  Preparing: INSERT INTO `test_1`.`blog` (`id`, `name`) VALUES (?, ?);
      //==> Parameters: 2(String), lengxf2(String)
      //      <==    Updates: 1
      //      ==>  Preparing: INSERT INTO `test_1`.`blog` (`id`, `name`) VALUES (?, ?);
      //==> Parameters: 3(String), lengxf3(String)
      //      <==    Updates: 1
      //      ==>  Preparing: INSERT INTO `test_1`.`blog` (`id`, `name`) VALUES (?, ?);
      //==> Parameters: 4(String), lengxf4(String)
      //      <==    Updates: 1
      //      ==>  Preparing: INSERT INTO `test_1`.`blog` (`id`, `name`) VALUES (?, ?);
      //==> Parameters: 5(String), lengxf5(String)
      //      <==    Updates: 1
      //      ==>  Preparing: INSERT INTO `test_1`.`blog` (`id`, `name`) VALUES (?, ?);
      //==> Parameters: 6(String), lengxf6(String)
      //      <==    Updates: 1
      //      ==>  Preparing: INSERT INTO `test_1`.`blog` (`id`, `name`) VALUES (?, ?);
      //==> Parameters: 7(String), lengxf7(String)
      //      <==    Updates: 1
      //      ==>  Preparing: INSERT INTO `test_1`.`blog` (`id`, `name`) VALUES (?, ?);
      //==> Parameters: 8(String), lengxf8(String)
      //      <==    Updates: 1
      //      ==>  Preparing: INSERT INTO `test_1`.`blog` (`id`, `name`) VALUES (?, ?);
      //==> Parameters: 9(String), lengxf9(String)
      //      <==    Updates: 1
    configuration.setDefaultExecutorType(ExecutorType.SIMPLE);

      //==>  Preparing: INSERT INTO `test_1`.`blog` (`id`, `name`) VALUES (?, ?);
      //==> Parameters: 1(String), lengxf1(String)
      //<==    Updates: 1
      //==> Parameters: 2(String), lengxf2(String)
      //<==    Updates: 1
      //==> Parameters: 3(String), lengxf3(String)
      //<==    Updates: 1
      //==> Parameters: 4(String), lengxf4(String)
      //<==    Updates: 1
      //==> Parameters: 5(String), lengxf5(String)
      //<==    Updates: 1
      //==> Parameters: 6(String), lengxf6(String)
      //<==    Updates: 1
      //==> Parameters: 7(String), lengxf7(String)
      //<==    Updates: 1
      //==> Parameters: 8(String), lengxf8(String)
      //<==    Updates: 1
      //==> Parameters: 9(String), lengxf9(String)
      //<==    Updates: 1
    //configuration.setDefaultExecutorType(ExecutorType.REUSE);


     //==>  Preparing: INSERT INTO `test_1`.`blog` (`id`, `name`) VALUES (?, ?);
     // ==> Parameters: 1(String), lengxf1(String)
     // ==> Parameters: 2(String), lengxf2(String)
     // ==> Parameters: 3(String), lengxf3(String)
     // ==> Parameters: 4(String), lengxf4(String)
     // ==> Parameters: 5(String), lengxf5(String)
     // ==> Parameters: 6(String), lengxf6(String)
     // ==> Parameters: 7(String), lengxf7(String)
     // ==> Parameters: 8(String), lengxf8(String)
     // ==> Parameters: 9(String), lengxf9(String)
    //configuration.setDefaultExecutorType(ExecutorType.BATCH);

    // addmapper 需要放在最后执行
    //防止其他配置项不生效
    configuration.addMapper(BlogMapper.class);

    //构建 工厂
    //此处便为mybatis的核心初始化构建
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
    long start = System.currentTimeMillis();
    try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
      //这个地方为实际的调用逻辑
      BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
      IntStream.range(1, 10).forEach(e -> {
        String s = String.valueOf(e);
        mapper.insert(new Blog(s, "lengxf" + s));
        //mapper.selectBlog(1);
      });
      //Blog blog = mapper.selectBlog(101);
      //Blog blog1 = mapper.selectBlo1(101);
      //System.out.println(blog);
      //System.out.println(blog1);
      sqlSession.commit();
    }
    long consume = System.currentTimeMillis() - start;
    System.out.println("花费时间 : " + consume);
  }


}
